package com.promis.widget.mediapicker.data;

import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;

import com.promis.widget.R;
import com.promis.widget.mediapicker.entity.FolderParcelable;
import com.promis.widget.mediapicker.entity.VideoParcelable;
import com.promis.widget.mediapicker.util.FileUtils;
import com.promis.widget.mediapicker.util.FolderUtils;

import java.util.ArrayList;


/**
 * Created by dmcBig on 2017/6/9.
 */

public class VideoLoader implements LoaderManager.LoaderCallbacks{
    String[] MEDIA_PROJECTION = {
            MediaStore.Files.FileColumns.DATA,
            MediaStore.Files.FileColumns.DISPLAY_NAME,
            MediaStore.Files.FileColumns.DATE_ADDED,
            MediaStore.Files.FileColumns.MEDIA_TYPE,
            MediaStore.Files.FileColumns.SIZE,
            MediaStore.Files.FileColumns._ID ,
            MediaStore.Files.FileColumns.PARENT};

    Context mContext;
    DataCallback mLoader;
    public VideoLoader(Context context, DataCallback loader){
        this.mContext=context;
        this.mLoader=loader;
    }

    @Override
    public Loader onCreateLoader(int picker_type, Bundle bundle) {
        String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;

        Uri queryUri = MediaStore.Files.getContentUri("external");
        CursorLoader cursorLoader = new CursorLoader(
                mContext,
                queryUri,
                MEDIA_PROJECTION,
                selection,
                null, // Selection args (none).
                MediaStore.Files.FileColumns.DATE_ADDED + " DESC" // Sort order.
        );
        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader loader, Object o) {
        ArrayList<FolderParcelable> folders =new ArrayList<>();
        FolderParcelable allFolder =new FolderParcelable(mContext.getResources().getString(R.string.all_video));
        folders.add(allFolder);
        Cursor cursor=(Cursor) o;
        //Log.e("dmc",cursor.getCount()+"数量数量");
        while (cursor.moveToNext()){
            String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA));
            String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME));
            long dateTime = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATE_ADDED));
            int mediaType = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MEDIA_TYPE));
            long size= cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE));
            int id= cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID));

            if (size < 1) continue;
            String dirName = FolderUtils.getParent(path);
            long duration = FileUtils.getDuration(path);
            VideoParcelable media = new VideoParcelable(path,name,dateTime,size,id,dirName, duration);
            allFolder.addMedias(media);

            int index = FolderUtils.hasDir(folders,dirName);
            if(index!=-1){
                folders.get(index).addMedias(media);
            }else{
                FolderParcelable folder =new FolderParcelable(dirName);
                folder.addMedias(media);
                folders.add(folder);
            }
        }
        mLoader.onData(folders);
        cursor.close();
    }


    @Override
    public void onLoaderReset(Loader loader) {

    }
}
